//
//  AddFriend.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/15.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "AddFriend.h"
#import "WinkAlertView.h"
#import "TelephonyInfo.h"

@implementation AddFriend

+(AddFriend *)sharedInstance
{
    static AddFriend *addFriend;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        addFriend = [[AddFriend alloc] init];
    });
    return addFriend;
}

-(void)addFreindForJid:(NSArray *)jidArray
{
    AFHTTPRequestOperationManager *afManager = [AFHTTPRequestOperationManager manager];
    
    afManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    afManager.responseSerializer.acceptableContentTypes = [afManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    //提出國碼加帳號 密碼
    NSString *account = [TelephonyInfo accountAddCountryCode];
    
    NSString *friendJD = [jidArray componentsJoinedByString:@","];;

    NSDictionary *parameters = @{@"type":@"add_friend",@"secret":@"openfire",@"username":account,@"friend":friendJD};
    
    NSLog(@"%@",parameters);
    
    
    [afManager POST:MD_SERVER_API parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"JSON: %@", responseObject);
        
        NSString *responseResult = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
        //成功
        if ([responseResult isEqualToString:@"0"]) {
            
            //loading 結束
            
            [[[UIAlertView alloc]initWithTitle:@"" message:@"好友邀請已送出" delegate:self cancelButtonTitle:@"確認" otherButtonTitles: nil]show];
            //成功後 重新去更新朋友清單
            [[XMPPSharedInstance sharedInstance]getFriendRoster];
        }else if ([responseResult isEqualToString:@"3"])
        {
            [[[UIAlertView alloc]initWithTitle:@"" message:@"好友邀請已送出" delegate:self cancelButtonTitle:@"確認" otherButtonTitles: nil]show];
            //成功後 重新去更新朋友清單
            [[XMPPSharedInstance sharedInstance]getFriendRoster];

        }else
        {
            [WinkAlertView _showAlertViewWithTitle:@"失敗" content:responseObject[@"msg"] cancelButtonTitle:@"確認" showTime:3.0f];
            if (self.delegate && [self.delegate respondsToSelector:@selector(errorAddFriend)]) {
                [self.delegate errorAddFriend];
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        
        
        [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"網路連結失敗" cancelButtonTitle:@"確認" showTime:3.0f];
        if (self.delegate && [self.delegate respondsToSelector:@selector(errorAddFriend)]) {
            [self.delegate errorAddFriend];
        }
        
    }];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(AddFriendResult:)]) {
            [self.delegate AddFriendResult:nil];
        }
    }
}
@end
